﻿#include "uideviceinfocard.h"

#include <QObject>
#include <QLabel>
#include <QHBoxLayout>
#include <QPushButton>
#include <QDebug>
#include <QPainter>
#include <QFormLayout>
#include <QTextEdit>

#include <shlib/quc_sdk/iconhelper/icon_helper.h>
#include <shlib/quc_sdk/lightbutton/lightbutton.h>
#include <model/device/uideviceinfo.h>
#include <shlib/csigmanager.h>
#include <ElaText.h>

UiDeviceInfoCard::UiDeviceInfoCard(CRtDeviceInfo &info, QWidget *parent)
    : QWidget{parent},m_info(info)
{
    uiInit();
    reloadData();
}

void UiDeviceInfoCard::uiInit()
{
    // IconHelper* iconsMgr = IconHelper::instance();
    // m_devIcon->setIcon(QIcon(iconsMgr->getPixmap(QColor::fromString("#1e3050"), IconHelper::FontAwesome6Solid, 0xf8da, 55, 69,69)));
    // connect(m_devIcon, &QPushButton::clicked, this, [&](){
    //     UiDeviceInfo* window = new UiDeviceInfo;
    //     window->setObjectName(windowNameMap.value(EWindowType::EditDevice));
    //     bool exists = false;
    //     // CSigMgr.sigFindExistsWidget(EWindowType::EditDevice, exists);
    //     // exists ? CSigMgr.sigDelWidget(EWindowType::EditDevice, EWindowType::EditDevice, window)
    //     //        : CSigMgr.sigAddWidget(window, EWindowType::EditDevice);
    // });

    QVBoxLayout* vlyout = new QVBoxLayout;
    QVBoxLayout* hacclyout = new QVBoxLayout;
    QVBoxLayout* vlaftlyout = new QVBoxLayout;

    QHBoxLayout* infoLayout = new QHBoxLayout;

    lab_descr= new ElaText(this);
    lab_descr->setAlignment(Qt::AlignLeft);
    QHBoxLayout* hlayout = new QHBoxLayout;
    hlayout->addWidget(lab_descr);
    vlyout->addLayout(hlayout);

    ElaText* lab = new ElaText("所属模型:",this);
    QFont font = lab->font();
    font.setPointSize(14);
    lab->setFont(font);
    lab->setFixedWidth(90);
    lab_modelname = new ElaText;
    lab_modelname->setFont(font);
    lab_modelname->setAlignment(Qt::AlignLeft);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(lab);
    hlayout->addWidget(lab_modelname);
    vlaftlyout->addLayout(hlayout);

    lab = new ElaText("设备名字:",this);
    lab->setFont(font);
    lab->setFixedWidth(90);
    lab_name = new ElaText;
    lab_name->setFont(font);
    lab_name->setAlignment(Qt::AlignLeft);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(lab);
    hlayout->addWidget(lab_name);
    vlaftlyout->addLayout(hlayout);

    lab = new ElaText("设备分组:",this);
    lab->setFont(font);
    lab->setFixedWidth(90);
    lab_devicegroup = new ElaText;
    lab_devicegroup->setFont(font);
    lab_devicegroup->setAlignment(Qt::AlignLeft);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(lab);
    hlayout->addWidget(lab_devicegroup);
    vlaftlyout->addLayout(hlayout);

    lab = new ElaText("安装位置:",this);
    lab->setFont(font);
    lab->setFixedWidth(90);
    lab_location = new ElaText;
    lab_location->setFont(font);
    lab_devicegroup->setAlignment(Qt::AlignLeft);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(lab);
    hlayout->addWidget(lab_location);
    vlaftlyout->addLayout(hlayout);

    lab = new ElaText("创建时间:",this);
    lab->setFont(font);
    lab->setFixedWidth(90);
    lab_createtime = new ElaText;
    lab_createtime->setFont(font);
    lab_createtime->setAlignment(Qt::AlignLeft);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(lab);
    hlayout->addWidget(lab_createtime);
    vlaftlyout->addLayout(hlayout);

    lab = new ElaText("通讯协议:",this);
    lab->setFont(font);
    lab->setFixedWidth(90);
    lab_protocol = new ElaText;
    lab_protocol->setFont(font);
    lab_protocol->setAlignment(Qt::AlignLeft);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(lab);
    hlayout->addWidget(lab_protocol);
    vlaftlyout->addLayout(hlayout);

    // lab = new ElaText("固件版本:",this);
    // lab->setFont(font);
    // lab->setFixedWidth(90);
    // lab_version = new ElaText;
    // lab_version->setFont(font);
    // lab_version->setAlignment(Qt::AlignLeft);
    // hlayout = new QHBoxLayout;
    // hlayout->addWidget(lab);
    // hlayout->addWidget(lab_version);
    // vlaftlyout->addLayout(hlayout);

    lab = new ElaText("累计在线:",this);
    lab->setFont(font);
    lab->setFixedWidth(90);
    lab_accruedonlinetime = new ElaText;
    lab_accruedonlinetime->setFont(font);
    lab_accruedonlinetime->setAlignment(Qt::AlignLeft);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(lab);
    hlayout->addWidget(lab_accruedonlinetime);
    vlaftlyout->addLayout(hlayout);

    lab = new ElaText("启用状态:",this);
    lab->setFont(font);
    lab->setFixedWidth(90);
    lab_valid = new ElaText;
    lab_valid->setFont(font);
    lab_valid->setAlignment(Qt::AlignLeft);
    hlayout = new QHBoxLayout;
    hlayout->addWidget(lab);
    hlayout->addWidget(lab_valid);
    vlaftlyout->addLayout(hlayout);

    infoLayout->addLayout(vlaftlyout, 2);

    QVBoxLayout* imgLayout = new QVBoxLayout;

    btn_img = new QPushButton(this);
    btn_img->setFixedHeight(200);

    QHBoxLayout* imgtmpLayout = new QHBoxLayout;
    imgtmpLayout->addStretch();
    imgLayout->addWidget(btn_img);
    imgtmpLayout->addStretch();

    imgLayout->addLayout(imgtmpLayout, 3);

    QGridLayout* gbtnlayout = new QGridLayout;
    btn_collector = new QPushButton("启动采集" ,this);
    gbtnlayout->addWidget(btn_collector, 0, 0);

    btn_run = new QPushButton("设备上电",this);
    gbtnlayout->addWidget(btn_run, 0, 1);

    btn_cfg = new QPushButton("通讯配置",this);
    gbtnlayout->addWidget(btn_cfg, 1, 0);

    btn_debug = new QPushButton("设备调试", this);
    gbtnlayout->addWidget(btn_debug, 1, 1);

    imgLayout->addLayout(gbtnlayout, 1);

    infoLayout->addLayout(imgLayout, 1);

    QFrame *line = new QFrame(this);
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Plain);
    vlyout->addWidget(line);
    vlyout->addLayout(infoLayout);

    this->setLayout(vlyout);

    connect(btn_cfg, &QPushButton::clicked, this, &UiDeviceInfoCard::slotCfgEdit);
}

void UiDeviceInfoCard::reloadData() {
    lab_name->setText(m_info.name);
    lab_descr->setText(m_info.descr);
    lab_modelname->setText(m_info.model_name);
    lab_devicegroup->setText(m_info.grouping);
    lab_location->setText(m_info.location);
    lab_protocol->setText(m_info.protocol);
    lab_createtime->setText(m_info.create_time.toString());
    lab_accruedonlinetime->setText(QString::number(m_info.accrued_online_time) + "小时");
    lab_valid->setText(m_info.valid ? "正常启用" : "设备停用");
}

CRtDeviceInfo UiDeviceInfoCard::info()
{
    return m_info;
}

void UiDeviceInfoCard::slotCfgEdit()
{
    SerialPortSettingsWidget* w = new SerialPortSettingsWidget;
    w->show();
}

void UiDeviceInfoCard::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QColor lightBlue(173, 216, 230);
    painter.save();
    painter.setPen(Qt::NoPen);
    painter.setBrush(lightBlue); // 设置画笔的填充颜色
    painter.drawRoundedRect(rect(), 10, 10);
    painter.restore();
}
